import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

// 静态路由  所有人可访问
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: { title: '数据大屏', icon: 'dashboard', affix: true }
      }
    ]
  },
  {
    path: '/profile',
    component: Layout,
    redirect: '/profile/index',
    hidden: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/profile/index'),
        name: 'Profile',
        meta: { title: 'Profile', icon: 'user', noCache: true }
      }
    ]
  }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
// 动态路由 受权限控制
export const asyncRoutes = [
  // 首页配置路由
  // 只有在rules内 有当前路由地址的用户可以访问
  {
    path: '/homepageManager',
    component: Layout,
    redirect: '/homepageManager/bannerSwiper',
    alwaysShow: true, // will always show the root menu
    name: 'homepageManager',
    meta: {
      title: '首页配置',
      icon: 'lock',
      roles: ['/homepageManager'] // you can set roles in root nav
    },
    children: [
      {
        path: 'bannerSwiper',
        component: () => import('@/views/homepageManager/bannerSwiper/index'),
        name: 'bannerSwiper',
        meta: {
          icon: 'lock',
          title: '轮播图管理',
          roles: ['/homepageManager/bannerSwiper'] // or you can only set roles in sub nav
        }
      },
      {
        path: 'recommend',
        component: () => import('@/views/homepageManager/recommend/index'),
        name: 'recommend',
        meta: {
          icon: 'lock',
          title: '种草管理',
          roles: ['/homepageManager/recommend']
          // if do not set roles, means: this page does not require permission
        }
      }
    ]
  },
  // 商品管理路由
  {
    path: '/productManager',
    component: Layout,
    redirect: '/productManager/productList',
    name: 'productManager',
    meta: {
      title: '商品管理',
      icon: 'el-icon-s-shop',
      roles: ['/productManager']
    },
    children: [
      {
        path: 'productList',
        name: 'productList',
        component: () => import('@/views/productManager/productList/index'),
        meta: {
          title: '商品列表',
          icon: 'el-icon-goods',
          roles: ['/productManager/productList']
        }
      },
      {
        path: 'productCategory',
        name: 'productCategory',
        component: () => import('@/views/productManager/productCategory/index'),
        meta: {
          title: '商品分类',
          icon: 'el-icon-document-copy',
          roles: ['/productManager/productCategory']
        }
      },
      {
        path: 'productCoupons',
        name: 'productCoupons',
        component: () => import('@/views/productManager/productCoupons/index'),
        meta: {
          title: '优惠券管理',
          icon: 'el-icon-wallet',
          roles: ['/productManager/productCoupons']
        }
      },
      {
        path: 'addDetail',
        name: 'addProductDetail',
        hidden: true, // 不在左侧导航栏显示
        component: () => import('@/views/productManager/detail/index'),
        meta: {
          title: '新增商品',
          icon: 'table',
          activeMenu: '/product/list',
          componentUrl: '@/views/productManager/detail/index',
          roles: ['/productManager/addDetail']
        }
      },
      {
        path: 'evaluate',
        name: 'evaluate',
        component: () => import('@/views/productManager/evaluate/index'),
        meta: {
          title: '评价管理',
          icon: 'el-icon-wallet',
          roles: ['/productManager/evaluate']
        }
      }
    ]
  },
  // 订单管理路由

  // 权限管理路由
  {
    path: '/permissionMember',
    name: 'permissionMember',
    component: Layout,
    redirect: '/userMember/orderMember',
    meta: {
      title: '权限管理',
      icon: 'el-icon-key',
      roles: ['/permissionMember']
    },
    children: [
      {
        path: 'orderMember',
        name: 'orderMember',
        component: () => import('@/views/userMember/orderMember/index'),
        meta: {
          title: '会员管理',
          icon: 'el-icon-user',
          roles: ['/permissionMember/orderMember']
        }
      },
      {
        path: 'roleManagement',
        name: 'roleManagement',
        component: () => import('@/views/userMember/roleManagement/index'),
        meta: {
          title: '角色管理',
          icon: 'el-icon-user',
          roles: ['/permissionMember/roleManagement']
        }
      },
      {
        path: 'identity',
        name: 'identity',
        component: () => import('@/views/userMember/identity/index'),
        meta: {
          title: '权限管理',
          icon: 'el-icon-user',
          roles: ['/permissionMember/identity']
        }
      },
      {
        path: 'adminManagement',
        name: 'adminManagement',
        component: () => import('@/views/userMember/adminManagement/index'),
        meta: {
          title: '用户管理',
          icon: 'el-icon-user',
          roles: ['/permissionMember/adminManagement']
        }
      }
    ]
  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
